import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tv_friso_screen/utils/result_check_utils.dart';
import 'package:tv_friso_screen/utils/share_pre_utils.dart';

import 'data_model/token_bean.dart';
import 'model/response_with_object.dart';
import 'net/api_address.dart';
import 'net/data_helper.dart';
import 'net/http_manager.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  var phoneController = TextEditingController();
  var pwdController = TextEditingController();
  var loginController = TextEditingController();

  FocusNode focusNodeUser = FocusNode();
  FocusNode focusNodePwd = FocusNode();
  FocusNode focusNodeLogin = FocusNode();

  //登录
  void postLogin() async {
    var params = {
      "login_account": phoneController.text,
      "password": pwdController.text,
    };
    var formData = DataHelper.getBaseMapWithParamsWithoutToken(
        params); //DataHelper.getBaseMapParamsWithoutToken(params);
    var response =
        await HttpManager().post(ApiAddress.postLogin, params: formData);

    if (mounted) {
      if (response['data'] != null &&
          response['code'] == HttpManager.codeSuccess) {
        ResponseWithObject<TokenBean> data =
            ResponseWithObject.fromJson(response as Map<String, dynamic>, (j) {
          if (j != null) {
            return TokenBean.fromJson(j);
          } else {
            return j;
          }
        });

        //给Token赋值
        SharePreUtils.setToken(data.data.accessToken);

        //跳转首页
        // Navigator.of(context).pushAndRemoveUntil(
        //     MaterialPageRoute(builder: (context) => const HomePage()),
        //     (route) => false);

        // Navigator.of(context).pushAndRemoveUntil(
        //     MaterialPageRoute(builder: (context) => const HomePageTest()),
        //     (route) => false);
      } else {
        ResultCheckUtils.checkResult(
            context, response['code'], response['msg']);
      }
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    phoneController.text = "18851766920";
    pwdController.text = "Fcg123456";
    loginController.text = "登 录";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        alignment: Alignment.center,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage(
              "assets/images/icon_bg_login.jpg",
            ),
            fit: BoxFit.fill,
          ),
        ),
        child: Stack(
          children: [
            SizedBox(
              width: 528.w,
              height: 484.w,
              child: Text(
                "登录",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 28.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Container(
              width: 528.w,
              height: 484.w,
              padding: EdgeInsets.all(80.w),
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(
                    "assets/images/icon_login_back.png",
                  ),
                  fit: BoxFit.fill,
                ),
              ),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      width: double.infinity,
                      margin: EdgeInsets.only(top: 60.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(5.w)),
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          SizedBox(
                            width: 25.w,
                          ),
                          Image.asset(
                            "assets/images/icon_user.png",
                            width: 25.w,
                            height: 28.w,
                          ),
                          SizedBox(
                            width: 200.w,
                            height: 50.h,
                            child: TextField(
                              onChanged: (value) {},
                              onEditingComplete: () {},
                              onSubmitted: (value) {},
                              keyboardType: TextInputType.phone,
                              textAlign: TextAlign.left,
                              enabled: true,
                              controller: phoneController,
                              style: TextStyle(
                                color: const Color(0xff333333),
                                fontSize: 17.sp,
                              ),
                              //将焦点绑定到TextFiled
                              decoration: InputDecoration(
                                isCollapsed: false,
                                hintText: "请输入账户",
                                hintStyle: TextStyle(
                                  fontSize: 17.sp,
                                  color: const Color(0xff999999),
                                ),
                                contentPadding: EdgeInsets.only(
                                    left: 24.w, right: 20.w, top: 20.w),
                                border: const OutlineInputBorder(
                                    borderSide: BorderSide.none),
                              ),
                              cursorColor: Colors.blue,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      margin: EdgeInsets.only(top: 30.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(5.w)),
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          SizedBox(
                            width: 25.w,
                          ),
                          Image.asset(
                            "assets/images/icon_pwd.png",
                            width: 25.w,
                            height: 28.w,
                          ),
                          SizedBox(
                            width: 200.w,
                            height: 50.h,
                            child: TextField(
                              onChanged: (value) {},
                              onEditingComplete: () {},
                              onSubmitted: (value) {},
                              textAlign: TextAlign.left,
                              enabled: true,
                              controller: pwdController,
                              style: TextStyle(
                                color: const Color(0xff333333),
                                fontSize: 17.sp,
                              ),
                              decoration: InputDecoration(
                                isCollapsed: false,
                                hintText: "请输入密码",
                                hintStyle: TextStyle(
                                  fontSize: 17.sp,
                                  color: const Color(0xff999999),
                                ),
                                contentPadding: EdgeInsets.only(
                                    left: 24.w, right: 20.w, top: 20.w),
                                border: const OutlineInputBorder(
                                    borderSide: BorderSide.none),
                              ),
                              cursorColor: Colors.blue,
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 30.w,
                    ),
                    MaterialButton(
                      onPressed: () {
                        // print("点击按钮------- ");
                        postLogin();
                      },
                      minWidth: double.infinity,
                      height: 62.w,
                      color: Colors.blue,
                      child: Text(
                        "登 录",
                        style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 24.sp,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
